package ar.com.sudoku.rules;

import java.util.ArrayList;
import java.util.List;

import ar.com.sudoku.model.SudokuNumber;
import ar.com.sudoku.model.Grid;

public class SquareRule implements Rule {

	@Override
	public boolean apply(Grid grid) {
		List<SudokuNumber> square = new ArrayList<SudokuNumber>();
		short[] squares = new short[3];
		squares[0] = 1;
		squares[1] = 4;
		squares[2] = 7;
		for (short xSquare : squares) {
			for (short ySquare : squares) {
				for (short x = 0; x <=2; x ++) {
					for (short y = 0; y <=2; y ++) {
						SudokuNumber xSudokuNumber = SudokuNumber.valueOf("_" + (xSquare + x));
						SudokuNumber ySudokuNumber = SudokuNumber.valueOf("_" + (ySquare + y));
						if (grid.get(xSudokuNumber,ySudokuNumber) == null) {
							continue;
						}
						if (SudokuNumber._4.equals(grid.get(xSudokuNumber,ySudokuNumber))) {
							//System.out.println();;
						}
						if (square.contains(grid.get(xSudokuNumber,ySudokuNumber))) {
							return false;
						} else  {
							square.add(grid.get(xSudokuNumber,ySudokuNumber));
						}
					}
				}
				square.clear();
			}
		}
		return true;
	}

}
